// import Vue from 'vue'

/**
 * 从sessionStorage中获取所有的buttons
 * */
function getButtons () {
  const buttonsSession = sessionStorage.getItem('buttons')
  if (!buttonsSession) {
    return []
  }
  const stringButton = Base64.decode(buttonsSession)
  const buttons = JSON.parse(stringButton)

  return buttons
}

/**
 * 当前按钮是否在当前角色配置的按钮中
 * */
function permissionInclude (value, arr) {
  const bool = arr.some(item => {
    if (item.code === value) {
      return true
    }
  })
  return bool
}

/**
 * 按钮权限，类似于v-if
 * */
const hasPermission = Vue.directive('hasPermission', {
  bind(el, binding, vnode){
    const buttons = getButtons()
    if(buttons && buttons.length && !permissionInclude(binding.value, buttons)){
      Vue.nextTick(() => el.parentNode.removeChild(el))
    }
  }
})


export default hasPermission
